package com.ruoyi.kong.api;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.kong.domain.KongHttpLog;
import com.ruoyi.kong.domain.KongLogInfo;
import com.ruoyi.kong.service.IKongHttpLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/http/log/")
public class ApiHttpLogInfoController extends BaseController {

    @Autowired
    private IKongHttpLogService kongHttpLogService;

    @PostMapping("/infos")
    public AjaxResult HttpLogRecode(HttpServletRequest request) {
        try {
            String req = IoUtil.read(request.getInputStream(), "UTF-8");
            logger.error("接收到请求:" + req);
            KongLogInfo kongLogInfo = new KongLogInfo();

            JSONObject reqJson = JSONUtil.parseObj(req);

            KongHttpLog kongHttpLog = null;

            if (reqJson.containsKey("request")) {
                String requestInfo = reqJson.getStr("request");
                kongLogInfo.setRequestInfo(requestInfo);
            }

            if (reqJson.containsKey("latencies")) {
                String latencies = reqJson.getStr("latencies");
                kongLogInfo.setLatencies(latencies);
            }

            if (reqJson.containsKey("response")) {
                String responseInfo = reqJson.getStr("response");
                kongLogInfo.setResponseInfo(responseInfo);
            }

            if (reqJson.containsKey("service")) {
                String service = reqJson.getStr("service");
                kongLogInfo.setService(service);
            }

            if (reqJson.containsKey("route")) {
                String route = reqJson.getStr("route");
                kongLogInfo.setRoute(route);
            }

            if (reqJson.containsKey("consumer")) {
                String consumer = reqJson.getStr("consumer");
                kongLogInfo.setConsumer(consumer);
            }

            if (reqJson.containsKey("tries")) {
                String tries = reqJson.getStr("tries");
                kongLogInfo.setTries(tries);
            }

            kongHttpLog = kongHttpLogService.getBeanFromJsonStr(kongLogInfo);

            if (ObjectUtil.isNotEmpty(kongHttpLog)) {

                if (reqJson.containsKey("client_ip")) {
                    kongHttpLog.setClientIp(reqJson.getStr("client_ip"));
                }
                if (reqJson.containsKey("upstream_uri")) {
                    kongHttpLog.setUpstreamUri(reqJson.getStr("upstream_uri"));
                }

                kongHttpLogService.insertKongHttpLog(kongHttpLog);
            }



        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return  success();
    }

    @GetMapping("/test")
    public AjaxResult HttpLogGet(){
        return  success("这是服务端返回数据！！！");
    }


}
